/*******************************************************************************
 * Copyright (c) 2012 Clever Devices.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Contributors:
 * 	Clever Devices Interns - 
 * 	Alex Frankel, Andria Young, Kevin Thomas, Nicholas Culpepper, Taner Oztimurlenk, Thomas Volberg
 ******************************************************************************/
package com.framework.cleverstop_tentative;

import android.app.Activity;
import android.app.FragmentManager;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.RelativeLayout;

import android.widget.Spinner;

/**
 * Initial setup page used to select TA and "sign in" to the users
 * BusTime account on the given TA's website
 * 
 * @author AFrankel
 *
 */
public class Splash extends Activity implements OnItemSelectedListener, OnClickListener{
	
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.splash);
		
		Drawable d = this.getResources().getDrawable(R.drawable.layout_background);
		getActionBar().setBackgroundDrawable(d);
		
		Thread timer = new Thread(){
			public void run(){
				try{
					sleep(2500);
				}catch(InterruptedException ex){
					ex.printStackTrace();
				}
			}
		};
		timer.start();

		
		taList = (Spinner) findViewById(R.id.taList);
		signIn = (Button) findViewById(R.id.bSignIn);
		next = (Button) findViewById(R.id.bContinue);
		//taList.setBackgroundColor(Color.GRAY);
		background = (RelativeLayout) findViewById(R.id.splashLayout);

		
		next.setOnClickListener(this);
		signIn.setOnClickListener(this);
		
		String [] cities = {"(Select City)","Chicago","New York","Niagara"};
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,cities);
		taList.setAdapter(adapter);
		
		taList.setOnItemSelectedListener(this);
		
	}

	@Override
	//Outlines what to do when this activity starts another activity and is
	//literally paused.  See activity life cycle for greater detail
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		finish();
	}
	

	
	
	public void onItemSelected(AdapterView<?> parent, View view, int pos,
			long id) {
		// TODO Auto-generated method stub
		switch(pos){
		case NOSELECTION:
			background.setBackgroundResource(R.drawable.splashmain);
			break;
		case CHICAGO:
			background.setBackgroundResource(R.drawable.chicago2);
			//background.setBackgroundColor(Color.BLUE);
			break;
		case NEWYORK:
			background.setBackgroundResource(R.drawable.ny8);
			//background.setBackgroundColor(Color.CYAN);
			break;
		case NIAGARA:
			background.setBackgroundResource(R.drawable.niagara2);
			//background.setBackgroundColor(Color.GREEN);
			break;
		}	
	}

	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
		
	}
	
	public void onClick(View v) {
		int buttonId = v.getId();
		
		switch(buttonId){
		case R.id.bContinue:
			Intent i = new Intent(Splash.this, BusTimeMainActivity.class);
			startActivity(i);
			break;
			
		case R.id.bSignIn:
			showBusTimeFragment();
			break;
		}
	}
	
	private void showBusTimeFragment(){
		FragmentManager fm = getFragmentManager();
		BusTimeLoginFragment bt = new BusTimeLoginFragment(taList.getSelectedItem().toString());
		bt.show(fm, "fragment_bustime");
	}
	
	private static final int NOSELECTION = 0;
	private static final int CHICAGO = 1;
	private static final int NEWYORK = 2;
	private static final int NIAGARA = 3;
	
	Button next;
	Button signIn;
	
	Spinner taList;
	RelativeLayout background;


}
